use crate::resp::HttpResponse;
use reqwest::Client;
use snafu::ResultExt;
use std::time::Duration;

pub fn client_with_default_timeout(secs: u64) -> crate::Result<Client> {
    let client = Client::builder()
        .timeout(Duration::from_secs(secs))
        .user_agent("bootbaton/0.1")
        .gzip(true)
        .build()
        .whatever_context("failed to build client")?;
    Ok(client)
}

pub async fn execute(rb: reqwest::RequestBuilder) -> crate::Result<HttpResponse> {
    let resp = rb.send().await.whatever_context("http request failed")?;
    let status = resp.status().as_u16();
    let headers = resp.headers().clone();

    let body = resp
        .bytes()
        .await
        .whatever_context("http response failed")?;

    Ok(HttpResponse {
        status,
        headers,
        body,
    })
}
